//
//  NMPOCommon.swift
//  NMPO
//
//  Created by PowerfullClean on 2023/10/31.
//

import UIKit
import SnapKit


let nmpo_SCREEN_WIDTH  =  UIScreen.main.bounds.size.width
let nmpo_SCREEN_HEIGHT =  UIScreen.main.bounds.size.height
class NMPOCommon: NSObject {
    @objc static func nmpo_getHXColorValue(rgb: UInt,a:CGFloat = 1) -> UIColor {
        return UIColor(
            red: CGFloat((rgb & 0xFF0000) >> 16) / 255.0,
            green: CGFloat((rgb & 0x00FF00) >> 8) / 255.0,
            blue: CGFloat(rgb & 0x0000FF) / 255.0,
            alpha: a
        )
    }
    
    @objc static var nmpo_statusBarHeight: CGFloat {
        return nmpo_isVirtualTop ? 44 : 20
    }
    @objc static var nmpo_isVirtualTop: Bool {
        if #available(iOS 11.0, *) {
            let keyWindow: UIWindow? = NMPOAppDelegate.nmpo_shared().nmpo_window ?? nil
            if keyWindow != nil {
                let topSafeInset: CGFloat = keyWindow!.safeAreaInsets.top
                if topSafeInset > 20.0 {
                    return true
                }
            }
        }
        return false
    }
    
    @objc static var nmpo_XBottomSpace: CGFloat {
        return nmpo_isVirtualHome ? 34 : 0
    }
    
    @objc static var nmpo_tabbarH: CGFloat {
        return nmpo_XBottomSpace+49
    }
    @objc static var nmpo_isPhone6: Bool {
        return nmpo_SCREEN_HEIGHT == 667
    }
    @objc static var nmpo_isVirtualHome: Bool {
        if #available(iOS 11.0, *) {
            let keyWindow: UIWindow? = NMPOAppDelegate.nmpo_shared().nmpo_window ?? nil
            if keyWindow != nil {
                let bottomSafeInset: CGFloat = keyWindow!.safeAreaInsets.bottom
                if bottomSafeInset > 0.0 {
                    return true
                }
            }
        }
        return false
    }
    @objc static var nmpo_version: String {
        return Bundle.main.infoDictionary!["CFBundleShortVersionString"] as? String ?? ""
    }
    @objc static var nmpo_bundleIdentifier: String {
        return Bundle.main.bundleIdentifier ?? ""
        
    }
    @objc static var nmpo_buildVersion: String {
        return Bundle.main.infoDictionary!["CFBundleVersion"] as? String ?? ""
        
    }
    @objc static var nmpo_idfv: String {
        return UIDevice.current.identifierForVendor?.uuidString ?? ""
    }
    @objc static func nmpo_getTopViewController(vc: UIViewController?) -> UIViewController? {
        if let nav = vc as? UINavigationController {
            return nmpo_getTopViewController(vc: nav.visibleViewController)
        }
        else if let tab = vc as? UITabBarController, let selected = tab.selectedViewController {
            return nmpo_getTopViewController(vc: selected)
        }
        else if let persented = vc?.presentedViewController {
            return nmpo_getTopViewController(vc: persented)
        }
        return vc
    }
    static func nmpo_alertShow(nmpo_title: String, nmpo_message: String, nmpo_vc: UIViewController? = nil, nmpo_comlete:(()->())? = nil) {
        var nmpo_vc = nmpo_vc
        if nmpo_vc == nil {
            nmpo_vc = NMPOAppDelegate.nmpo_shared().nmpo_window?.rootViewController
        }
        DispatchQueue.main.asyncAfter(deadline: .now()+0.2) {
            if nmpo_isAlert {
                return
            }
            nmpo_isAlert = true
            let alert = UIAlertController(title: nmpo_title, message: nmpo_message, preferredStyle: .alert)
            let action1 = UIAlertAction.init(title: "Cancel", style: .destructive) { _ in
                nmpo_isAlert = false
            }
            let action2 = UIAlertAction.init(title: "Confirm", style: .default) { _ in
                nmpo_isAlert = false
                if (nmpo_comlete != nil) {
                    nmpo_comlete?()
                }else {
                    UIApplication.shared.open(URL(string: UIApplication.openSettingsURLString)!, options: [:], completionHandler: nil)
                }
            }
            alert.addAction(action1)
            alert.addAction(action2)
            nmpo_vc?.present(alert, animated: true)
        }
        
    }
    static var nmpo_isAlert: Bool = false
    @objc static let nmpo_navHegiht: CGFloat = nmpo_statusBarHeight+44+16
    //MARK: nmpo value change
    static func nmpo_changeSizeValue(size: Double)-> String {
        var f = ""
        var type = "KB/s"
        if (size == 0) {
            f = "0"
            type = "KB/s"
        }
        else if (size >= 1024 && size < pow(1024, 2)) {
            f = String(format: "%.0f", size/1024.00)
            type = "KB/s"
        }else if (size >= pow(1024, 2) && size < pow(1024, 3)) {
            f = String(format: "%.0f", size/pow(1024, 2))
            type = "MB/s"
        }else
        if (size >= pow(1024, 3)) {
            f = String(format: "%.0f", size/pow(1024, 3))
            type = "GB/s"
        }
        return f+type
    }
    
    static func nmpo_getSizeValue(size: CGFloat)-> (String, String) {
        var f = ""
        var type = "KB/s"
        if (size == 0) {
            f = "0"
            type = "KB/s"
        }
        else if (size >= 1024 && size < pow(1024, 2)) {
            f = String(format: "%.0f", size/1024.00)
            type = "KB/s"
        }else if (size >= pow(1024, 2) && size < pow(1024, 3)) {
            f = String(format: "%.0f", size/pow(1024, 2))
            type = "MB/s"
        }else
        if (size >= pow(1024, 3)) {
            f = String(format: "%.0f", size/pow(1024, 3))
            type = "GB/s"
        }
        return (f, type)
    }
    static func nmpo_getSystemStorageSize() -> String {
           var totalSpace: String = ""
           let fileManager = FileManager.default
           do {
               let systemRootURL = URL(fileURLWithPath: "/")
               let attributes = try fileManager.attributesOfFileSystem(forPath: systemRootURL.path)
               
               if let totalSize = attributes[FileAttributeKey.systemSize] as? Int64
                   {
                   totalSpace = ByteCountFormatter.string(fromByteCount: totalSize, countStyle: .file)
               
               }
           } catch {
               print("无法获取系统存储空间信息：\(error.localizedDescription)")
           }
           return totalSpace
       }
}
extension UIView {
    @objc static var nmpo_viewToString: String {
        return NSStringFromClass(self.classForCoder())
    }
    @objc var nmpo_viewGetVController: UIViewController? {
        var next = self.next
        while next != nil {
            if let vc = next as? UIViewController {
                return vc
            }
            next = next?.next
        }
        return nil
    }
    @objc func nmpo_addTap(target: Any? = nil, action: Selector) {
        self.isUserInteractionEnabled = true
        guard let target = target else {
            let tap = UITapGestureRecognizer.init(target: self, action: action)
            addGestureRecognizer(tap)
            return
        }
        let tap = UITapGestureRecognizer.init(target: target, action: action)
        addGestureRecognizer(tap)
    }
}

extension NSInteger {
    func nmpo_formatTime() -> String {
        let hours = self / 3600
        let minutes = (self % 3600) / 60
        let remainingSeconds = self % 60
        
        let formattedTime = String(format: "%02d:%02d:%02d", hours, minutes, remainingSeconds)
        return formattedTime
    }
}


